package cn.swenty.backstage.framework.taglib;

import java.util.List;

import cn.swenty.backstage.framework.common.tree.TreeNode;
import cn.swenty.backstage.framework.common.util.StringUtil;

public class ShowTreeTag extends SimpleTag {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String node;
	private String nodeAction;
	private Boolean openAll = false;
	private Boolean check = false;
	private String selectValues;
	private String width;
	private String height;
	private String bindingId;
	private String hideNodes;
	private String hideChekLevel;
	private String openLevel;
	private String checkAction;
	public String getCheckAction() {
		return checkAction;
	}

	public void setCheckAction(String checkAction) {
		this.checkAction = checkAction;
	}

	
	
	public String getHideChekLevel() {
		return hideChekLevel;
	}

	public void setHideChekLevel(String hideChekLevel) {
		this.hideChekLevel = hideChekLevel;
	}

	public String getOpenLevel() {
		return openLevel;
	}

	public void setOpenLevel(String openLevel) {
		this.openLevel = openLevel;
	}

	protected String getPrintStr() throws Exception {

		StringBuffer sb = new StringBuffer();
		
		this.addLine(sb, "<link href=\"" + this.webRoot()	+ "/asserts/js/zTree/css/zTreeStyle/zTreeStyle.css\" rel=\"stylesheet\" type=\"text/css\" />");
		this.addLine(sb, "<script language='Javascript' src='" + this.webRoot()	+ "/asserts/js/zTree/js/jquery.ztree.core-3.5.min.js'></script>");
		this.addLine(sb, "<script language='Javascript' src='" + this.webRoot()	+ "/asserts/js/zTree/js/jquery.ztree.excheck-3.5.min.js'></script>");
		this.addLine(sb, "<script language='javascript'>");
		this.addLine(sb, " function zTreeOnClick(event, treeId,treeNode){");
		//如果是单选且没有选中事件，选中就直接隐藏
		if(!check && StringUtil.isNull(nodeAction)){
			this.addLine(sb, "$('#_div_"+id+"').slideUp('fast'); ");
		}
		if(StringUtil.isNotNullTrim(nodeAction)){
			
			this.addLine(sb, " var flag_ = eval('"+nodeAction+"(treeNode)');");
			
			this.addLine(sb, " if(flag_==undefined||flag_){");
			this.addLine(sb, "$('#_div_"+id+"').slideUp('fast'); ");
			this.addLine(sb, " }");
			
		}
		this.addLine(sb, " }");
		
		
		this.addLine(sb, " function zTreeOnCheck(event, treeId,treeNode){");
		if(StringUtil.isNotNullTrim(checkAction) && check){
			this.addLine(sb, " eval('"+checkAction+"(treeNode)')");
		}
		this.addLine(sb, " }");
		
		this.addLine(sb, " var  setting={isSimpleData : true,data:{key:{name:'name'},simpleData :{enable:true,idKey : 'id',pIdKey : 'pid',rootPId : '0'}},check: {enable:"+this.check+",nocheckInherit: false,chkboxType :{ \"Y\" : \"p\", \"N\" : \"s\" }},callback:{onClick:zTreeOnClick,onCheck:zTreeOnCheck}}; var data = []; var tree;");
		// 如果有回调
		if (StringUtil.isNotNullTrim(selectValues)) {
			this.addLine(sb, " function "+selectValues+"(){var nodes = tree.getCheckedNodes(true);var msg = [];for (var i = 0; i < nodes.length; i++) { msg.push(nodes[i])} return msg;}");
		}

		List<TreeNode> nodes = (List<TreeNode>) this.getPageValue(pageContext, node);
		String jsData = "";
		for(TreeNode tn:nodes){
			jsData += addNode(tn, "");
		}
		
		this.addLine(sb,jsData);
		this.addLine(sb,
				"$(function(){tree =  $.fn.zTree.init($(\"#"+id+"\"), setting, data);"
						+(openAll?"tree.expandAll(true)":""));
		this.addLine(sb,"$('#"+bindingId+"').click(function(){");
		this.addLine(sb, "var obj=$('#"+bindingId+"');var objOffset=$('#"+bindingId+"').offset(); /*alert(objOffset.left+'-'+objOffset.top+'-'+obj.outerHeight()+'-'+obj.outerWidth());*/ $('#_div_"+id+"').offset({left:objOffset.left + 'px', top:objOffset.top + obj.outerHeight() + 'px'}).css({width:"+(StringUtil.isNotNullTrim(width)?width:"obj.outerWidth()+'px'")+"}).slideDown('fast');");
		this.addLine(sb,"})");
		if(StringUtil.isNotNull(hideNodes)){
			String[]hns = hideNodes.split(",");
			for (int i = 0; i<hns.length;i++){
				this.addLine(sb,"tree.removeNode(tree.getNodeByParam('id','"+hns[i]+"'))");
			}
		}
		
		//如果有隐藏的级别标识 且check为true
		if(StringUtil.isNotNull(hideChekLevel) && check){
			String [] hcls = hideChekLevel.split(",");
			this.addLine(sb, "var nodes=tree.getNodes(); var arr1 = findDown(nodes,[]);  ");
			for(String hcl:hcls){
				int h = Integer.parseInt(hcl);
				this.addLine(sb, " for(var m=0;m<arr1.length;m++) {");
					
				this.addLine(sb, "  if(arr1[m].level=="+h+"){");
				this.addLine(sb, " arr1[m].nocheck = true;tree.updateNode(arr1[m]); ");
				this.addLine(sb, " }");
				this.addLine(sb, " }");
				
			}
		}
		
		//如果有隐藏的级别标识 且check为true
		if(StringUtil.isNotNull(openLevel)){
			String [] hcls = openLevel.split(",");
			this.addLine(sb, "var nodes=tree.getNodes(); var arr1 = findDown(nodes,[]);  ");
			for(String hcl:hcls){
				int h = Integer.parseInt(hcl);
				this.addLine(sb, " for(var m=0;m<arr1.length;m++) {");
					
				this.addLine(sb, "  if(arr1[m].level=="+h+"){");
				this.addLine(sb, "  tree.expandNode(arr1[m],true,false)");
				this.addLine(sb, " }");
				this.addLine(sb, " }");
				
			}
		}
		
		this.addLine(sb,"});");
		this.addLine(sb, " function findDown(nodes,arr){ if(!arr){arr = []} for(var i=0;i<nodes.length;i++){ if(nodes[i].children){arr.push(findDown(nodes[i].children,arr));} arr.push(nodes[i]); }  return arr;}");
		this.addLine(sb, "</script>");
		this.addLine(sb,
				"<div id=\"_div_"+id+"\" style='display:none; position: absolute;border:1px solid #DFDFDF;z-index:9999;overflow:auto;background:#fff;"+(StringUtil.isNotNullTrim(height)?"height:"+height:"")+"'><ul id=\""+id+"\" class=\"ztree\"></ul></div>");
		return sb.toString();
	}

	public String addNode(TreeNode node,String js){
		
		js +=" data.push({id:'"+(node.getId().replace("'", "\\'"))+"',name:'"+(node.getName().replace("'", "\\'"))+"',pid:'"+(StringUtil.isNotNullTrim(node.getParentId())?node.getParentId().replace("'", "\\'"):"0")+"'});";
		if(node.getChildren()!=null&&node.getChildren().size()>0){
			for(TreeNode child:(List<TreeNode>)node.getChildren()){
				js+=addNode(child, "");
			}
		}
		return js;
	}
	
	public String getNode() {
		return node;
	}

	public void setNode(String node) {
		this.node = node;
	}

	public String getNodeAction() {
		return nodeAction;
	}

	public void setNodeAction(String nodeAction) {
		this.nodeAction = nodeAction;
	}

	public Boolean isOpenAll() {
		return openAll;
	}

	public void setOpenAll(Boolean openAll) {
		this.openAll = openAll;
	}

	public String getSelectValues() {
		return selectValues;
	}

	public void setSelectValues(String selectValues) {
		this.selectValues = selectValues;
	}

	public Boolean isCheck() {
		return check;
	}

	public void setCheck(Boolean isCheck) {
		this.check = isCheck;
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public String getBindingId() {
		return bindingId;
	}

	public void setBindingId(String bindingId) {
		this.bindingId = bindingId;
	}

	public Boolean getOpenAll() {
		return openAll;
	}

	public Boolean getCheck() {
		return check;
	}

	public String getHeight() {
		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public String getHideNodes() {
		return hideNodes;
	}

	public void setHideNodes(String hideNodes) {
		this.hideNodes = hideNodes;
	}
	
	
	
}
